{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea43d122",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy.stats as st\n",
    "import seaborn as sns\n",
    "sns.set_style('whitegrid')\n",
    "import matplotlib.pyplot as plt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14051551",
   "metadata": {},
   "outputs": [],
   "source": [
    "stm = pd.read_csv(r\"D:\\dasanxia\\steam.csv\")\n",
    "stm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf5278c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#这一步想去掉购买后又没玩的游戏\n",
    "#stm_play = stm.loc[stm['is_played'] == 'play']\n",
    "#stm_play\n",
    "stm.loc[stm['is_played']=='purchase','hours'] = 0.0\n",
    "stm#将买了的时间置0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7b8cfe4",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime1 = stm.iloc[:,[0,1,4]]#取指定列\n",
    "alaytime1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e33a7da",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime1=alaytime1.drop_duplicates(subset=['user_id', 'game_id'], keep='last')#删除重复的purchase，但是只买不玩游戏的不删，直接是0评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "360fe233",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ccdbf1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime2 = stm.iloc[:,[0,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a92bc592",
   "metadata": {},
   "outputs": [],
   "source": [
    "alaytime3=alaytime2.groupby(['user_id']).sum()#求相同userid的所有\n",
    "alaytime3.rename(columns={'hours':'allhours'},inplace=True)\n",
    "alaytime3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43a769ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "neww=pd.merge(alaytime1,alaytime3,left_on='user_id',right_on='user_id')#新表数据有时间和所有时间\n",
    "neww.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b66a147",
   "metadata": {},
   "outputs": [],
   "source": [
    "neww['new_value'] = 10*neww['hours'] /neww['allhours'] \n",
    "neww.fillna(0, inplace=True)\n",
    "neww.head(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a0f50c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#neww.to_csv(r\"D:\\Desk\\otherdata.csv\")\n",
    "userids=stm.user_id.unique()\n",
    "gameids=stm.game_id.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b549533",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import seaborn as sns \n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler(feature_range=(0,10))\n",
    "\n",
    "all_user_minmax=[]\n",
    "for id in userids:\n",
    "    alluser_ids =neww.loc[neww['user_id']==id]\n",
    "    #print(alluser_ids)\n",
    "    alluser_minmax=scaler.fit_transform(np.array(alluser_ids['new_value']).reshape(-1,1))\n",
    "    all_user_minmax.append(alluser_minmax)#对应用户对玩过的游戏打分,去除最小最大\n",
    "all_user_minmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1200d039",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_user_mean=[]\n",
    "\n",
    "for val in all_user_minmax:\n",
    "    all_user_mean.append(val.mean())#求用户平均打分\n",
    "all_user_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c7780b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_user_syj=[]\n",
    "for id in userids:\n",
    "    all_user_syj.append(neww[neww['user_id']==id]['game_id'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a2d8acf",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_user_syj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1a03dcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_user_syj[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d35c3428",
   "metadata": {},
   "outputs": [],
   "source": [
    "same=set(all_user_syj[7439])&set(all_user_syj[3])#玩过相同的游戏\n",
    "same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43ab77f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "fenzi = 0\n",
    "for k  in same:\n",
    "    ruk = neww.loc[(neww['user_id']==7439)&(neww['game_id']==k)]['new_value'].values\n",
    "    rvk = neww.loc[(neww['user_id']==3)&(neww['game_id']==k)]['new_value'].values\n",
    "    fenzi+=ruk*rvk\n",
    "print(fenzi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3975c3eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#分母\n",
    "ruk1=0\n",
    "rvk1=0\n",
    "for k  in same:\n",
    "    ruk = neww.loc[(neww['user_id']==7439)&(neww['game_id']==k)]['new_value'].values\n",
    "    ruk1 +=ruk*ruk\n",
    "ruk2= pow(ruk1,0.5)\n",
    "for k  in same:\n",
    "    rvk = neww.loc[(neww['user_id']==3)&(neww['game_id']==k)]['new_value'].values\n",
    "    rvk1+=rvk*rvk\n",
    "rvk2= pow(rvk1,0.5)   \n",
    "funmu= ruk2*rvk2\n",
    "print(funmu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da764907",
   "metadata": {},
   "outputs": [],
   "source": [
    "fenzi/funmu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cbbc067",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eefdf0b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
